<template>
  <div>
    <van-search v-model="value" show-action placeholder="请输入搜索关键词">
      <template #left>
        <div @click="onClickButtonleft"><van-icon name="arrow-left" size="20" /></div>
      </template>
      <template #action>
        <div @click="onClickButton">搜索</div>
      </template>
    </van-search>
    <div>
      <div class="bgc" v-show="flag">
        <p>搜索历史</p>
        <span>
          <van-tag
            v-for="item in hotlist"
            :key="item"
            color="#ffe1e1"
            text-color="#ad0000"
            @click="godel2(item.keyword)"
            >{{ item.keyword }}</van-tag
          >
        </span>
      </div>

      <div class="bgc" v-show="flag">
        <p>热门搜索</p>
        <span>
          <van-tag
            v-for="item in hotlist"
            :key="item"
            color="#ffe1e1"
            text-color="#ad0000"
            @click="godel(item.keyword)"
            >{{ item.keyword }}</van-tag
          >
        </span>
      </div>
      <div>
        <ul>
          <li v-for="item in list" :key="item">
            <RouterLink :to="'/details/' + item.proid">
              <img :src="item.img1" alt="" />
              <p>{{ item.proname }}</p>
              <span>￥{{ item.originprice }}</span>
            </RouterLink>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { gethotword, getsearch, getseldel } from "../../service/user";
export default {
  data() {
    return {
      value: "",
      list: [],
      hotlist: [],
      flag: 1,
    };
  },
  created() {
    this.gethot();
  },
  methods: {
    //点击标签可直接前往查看此类商品列表
    onClickButton() {
      getsearch(this.value, 1, 10).then((res) => {
        if (res.data.code === "200") {
          console.log(res);
          this.list = res.data.data;
          //更改标签盒子是否可见
          this.flag = false;
        }
      });
    },
    onClickButtonleft() {
        history.back();
    },
    //获取热门推荐商品类
    gethot() {
      gethotword().then((res) => {
        console.log(res);
        if (res.data.code === "200") {
          this.hotlist = res.data.data;
        }
      });
    },
    //点击查看商品详情
    //动态路由
    godel(keyword) {
      getsearch(keyword, 1, 10).then((res) => {
        if (res.data.code === "200") {
          console.log(res);
          this.list = res.data.data;
          // this.flag=2
          this.flag = false;
        }
      });
    },
    godel2(keyword) {
      getsearch(keyword, 1, 10).then((res) => {
        if (res.data.code === "200") {
          this.list = res.data.data;
          this.flag = false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
span {
  margin: 10px;
}
.bgc {
  background-color: #f5f5f5;
}
p {
  font-size: 14px;
}

ul {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  li {
    width: 50%;
    box-sizing: border-box;
    padding: 10px;
    img {
      display: block;
      width: 100%;
    }
    p {
      width: 100%;
      font-size: 17px;
      overflow: hidden;
      height: 40px;
    }
    span {
      color: red;
      font-size: 16px;
    }
  }
}
a {
  color: black;
}
</style>
